package com.zzg.test2.web

import android.content.Context
import android.os.Build
import android.webkit.WebSettings
import android.webkit.WebView


class WebSetting {

    companion object {
        private const val TAG = "WebSetting"
        fun setWebSetting(webView: WebView) {
            val settings = webView.settings

            // 启用JavaScript
            settings.javaScriptEnabled = true

            // 优化缓存设置（保留缓存但控制大小）

            settings.setAppCacheEnabled(true) // 启用应用缓存
            settings.databaseEnabled = true // 启用数据库
            settings.domStorageEnabled = true // 启用DOM存储
            settings.cacheMode = WebSettings.LOAD_DEFAULT // 使用默认缓存策略
            settings.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
            // 限制缓存大小
            val cachePath = webView.context.cacheDir.absolutePath + "/web_cache"
            settings.setAppCachePath(cachePath)
            settings.setAppCacheMaxSize(100 * 1024 * 1024) // 限制为5MB

            // 设置数据库路径
            val dbPath = webView.context.getDir("web_db", Context.MODE_PRIVATE).path
            settings.databasePath = dbPath

            // 启用文件访问
            settings.allowFileAccess = true
            settings.allowContentAccess = true
            settings.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
//            // 针对华为设备的特殊处理
//            if (Build.MANUFACTURER.equals("huawei", ignoreCase = true)) {
//                // 华为设备使用兼容模式
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                    settings.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
//                }
//            }

            // 设置UserAgent
            val userAgent = settings.userAgentString + "/leadbank|APP|MerchmAppH5|5.5.2"
            settings.userAgentString = userAgent
        }

//        fun setWebSetting(webView: WebView) {
//
//            val mWebSettings: WebSettings = webView.getSettings()
//
//            mWebSettings.allowFileAccess = true
//            mWebSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
//            mWebSettings.setSupportZoom(true)
//            mWebSettings.builtInZoomControls = true
//            mWebSettings.useWideViewPort = true
//            mWebSettings.setSupportMultipleWindows(false)
//            mWebSettings.setAppCacheEnabled(true)
//            mWebSettings.domStorageEnabled = true
//            mWebSettings.javaScriptEnabled = true
//            mWebSettings.setGeolocationEnabled(true)
//            mWebSettings.setAppCacheMaxSize(Long.MAX_VALUE)
//            mWebSettings.setAppCachePath(webView.context.getDir("appcache", 0).getPath())
//            mWebSettings.databasePath = webView.context.getDir("databases", 0).getPath()
//            mWebSettings.setGeolocationDatabasePath(
//                webView.context.getDir("geolocation", 0)
//                    .getPath()
//            )
//            mWebSettings.pluginState = WebSettings.PluginState.ON_DEMAND
//            mWebSettings.loadWithOverviewMode = true
//            mWebSettings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
//            val mUserAgent = mWebSettings.userAgentString
//            mWebSettings.setUserAgentString("$mUserAgent App/AppName")
////        syncCookie()
//            mWebSettings.useWideViewPort = true
//            mWebSettings.loadWithOverviewMode = true
//
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//                mWebSettings.displayZoomControls = false
//            }
//            mWebSettings.loadsImagesAutomatically = true
//            mWebSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
////        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
////            CookieManager.getInstance()
////            .setAcceptThirdPartyCookies(mWebView, true)
//
//            webView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY)
//            webView.setHorizontalScrollBarEnabled(false)
//            webView.setHorizontalFadingEdgeEnabled(false)
//            webView.setVerticalFadingEdgeEnabled(false)
//
//            webView.requestFocus()
//        }

    }
}